﻿package smooth.core.object.youTubeVideoComponent {
	
	import flash.events.*;
	import flash.utils.Timer;
	import app.core.action.RotatableScalable;
	import app.core.element.*;
	import smooth.core.object.youTubeVideoPlayerAs3Bridge.YTPlayer;
	import smooth.core.object.youTubeVideoPlayerAs3Bridge.VideoDurationEvent;
	import smooth.core.object.youTubeVideoPlayerAs3Bridge.VideoCurrentTimeEvent;

	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.net.NetStream;
	import flash.net.NetConnection;
	import flash.media.SoundTransform;
	import flash.media.Video;
	import flash.net.URLRequest;
	import flash.net.URLVariables;

	public class YouTubeVideoPlayer extends RotatableScalable {
		// Constantes
		private static var YOUTUBE_URL_PLAYER:String = "http://gdata.youtube.com/apiplayer?enablejsapi=1";
		// Items
		private var clickgrabber:Shape = new Shape();
		private var border:Shape = new Shape();
		private var video1:YTPlayer;
		private var videoDuration:Number;
		private var videoSeek:int;
		private var play_btn:Sprite = new Sprite();
		private var pause_btn:Sprite = new Sprite();
		private var toggle_play:Number;
		private var _scrubbing:Boolean;
		private var _Vscrubbing:Boolean;
		private var _duration:Number;
		private var scrubSlider:HorizontalSlider;
		private var volumeSlider:HorizontalSlider;
		private var wrapper0:Wrapper; 		
		private var wrapper1:Wrapper; 
		private var loadInterval:Number;
				
		// Default values
		private var initialIdVideo:String = "";
		private var devKey:String = "";
		private var border_size:Number = 10.0;
		private var velX:Number = 0.0;
		private var velY:Number = 0.0;
		private var velAng:Number = 0.0;
		private var friction:Number = 0.85;
		private var angFriction:Number = 0.92;
		
		public function YouTubeVideoPlayer(id:String, devKey:String) {
			this.initialIdVideo = id;
			this.devKey = devKey;
			this.x = 1000 * Math.random() - 500;
			this.y = 1000 * Math.random() - 500;
			this.x = 0;
			this.y = 0;
			
			// Init du player
			this.initPlayer();
		}
		
		private function initPlayer():void {
			if (YTPlayer.getSharedState() == YTPlayer.FREE) {
				this.video1 = new YTPlayer(this.devKey);
				this.video1.setMonitorPlayback(true);
				this.video1.addEventListener(VideoDurationEvent.videoDuration, onVideoDuration);
				this.video1.addEventListener(VideoCurrentTimeEvent.videoCurrentTime, onVideoCurrentTime);
				this.video1.cueVideoById(this.initialIdVideo);
				this.video1.setPlayerSize(200, 150);
				// Init des visuels du composant			
				initGraphics();
				// Init des events du composant
				initEvents();	
				// Init des process du composant
				initProcess();
			} else {
				alarmTimer =new Timer(2,1);
				alarmTimer.addEventListener(TimerEvent.TIMER, tryAgainInitPlayer);
				alarmTimer.start();
			}
		}
		
		private function tryAgainInitPlayer(e:TimerEvent):void {
			this.initPlayer();
		}
				
		private function onVideoDuration(e:VideoDurationEvent):void {
			this.videoDuration = this.video1.getDuration();
		}
		
		private function onVideoCurrentTime(e:VideoCurrentTimeEvent):void {
			this.scrubSlider.setValue(this.video1.getCurrentTime()/this.videoDuration);
			this.scrubSlider.updateGraphics();
		}
		
		private function initGraphics():void {
			var videoWidth:int = 320;
			var videoHeight:int = 240;
			video1.x = -videoWidth/2;
			video1.y = -videoHeight/2;
			border.x = video1.x-border_size;
			border.y = video1.y-border_size;
						
			play_btn.graphics.beginFill(0x00CC00, 0.0);
			play_btn.graphics.drawRect(video1.x+2, video1.y+2, 30, 30);
			play_btn.graphics.endFill();												
			play_btn.graphics.beginFill(0x000000, 1);
			play_btn.graphics.moveTo(video1.x+8, video1.y+7);
			play_btn.graphics.lineTo(video1.x+8, video1.y+7);
			play_btn.graphics.lineTo(video1.x+28, video1.y+17);			
			play_btn.graphics.lineTo(video1.x+8, video1.y+27);			
			play_btn.graphics.lineTo(video1.x+8, video1.y+7);	
			play_btn.graphics.endFill();
			play_btn.blendMode="invert";
			
			pause_btn.graphics.beginFill(0xFFFFFF,0.0);
			pause_btn.graphics.drawRect(video1.x+2, video1.y+2, 30, 30);
			pause_btn.graphics.endFill();			
			pause_btn.graphics.beginFill(0x000000, 0.25);
			pause_btn.graphics.drawRect(video1.x+9, video1.y+7, 6, 20);
			pause_btn.graphics.drawRect(video1.x+19, video1.y+7, 6, 20);
			pause_btn.graphics.endFill();
			pause_btn.blendMode="invert";			
			
			clickgrabber.scaleX = videoWidth;
			clickgrabber.scaleY = videoHeight;
			clickgrabber.x = -videoWidth/2;
			clickgrabber.y = -videoHeight/2;

			scrubSlider = new HorizontalSlider(videoWidth,20);	
			volumeSlider = new HorizontalSlider(videoHeight+48,25);

			scrubSlider.x=-videoWidth/2;
			scrubSlider.y=videoHeight/2+5;		
			volumeSlider.x=videoWidth/2+12;			
			volumeSlider.y=videoHeight/2+38;
			volumeSlider.rotation-=90;	
			volumeSlider.alpha = 0.5;	
			
			scrubSlider.setValue(0.0);
			volumeSlider.setValue(0.5);
			
			clickgrabber.graphics.beginFill(0xffffff, 0.1);
			clickgrabber.graphics.drawRect(0, 0, 1, 1);
			clickgrabber.graphics.endFill();

			border.graphics.beginFill(0xffffff, 0.20);	
			border.graphics.lineStyle(1,0xffffff, 0.35);
			border.graphics.drawRect(0, 0, videoWidth+(border_size * 2), videoHeight+(border_size * 2)+25);
			border.graphics.endFill();

			addChild(border); 
			addChild(video1);
			addChild(clickgrabber);	

			this.wrapper0 = new Wrapper(play_btn); 	
			this.wrapper1 = new Wrapper(pause_btn); 
			trace(this.wrapper0.width);
			trace(this.wrapper0.height);
			trace(this.wrapper1.width);
			trace(this.wrapper1.height);
			
			addChild(wrapper0);
			addChild(wrapper1);		
		
			toggle_play = 0;
			pause_btn.visible = false;
			
			addChild(scrubSlider);			
			addChild(volumeSlider);		
		}
		
		private function initProcess():void {
		}
		
		private function initEvents():void {
			this.scrubSlider.addEventListener(TouchEvent.MOUSE_DOWN, onMouseDown);
			this.scrubSlider.addEventListener(TouchEvent.MOUSE_MOVE, onMouseMove);
			this.scrubSlider.addEventListener(TouchEvent.MOUSE_UP, onMouseUp);
			this.scrubSlider.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			this.scrubSlider.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			this.scrubSlider.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);	
			
			this.volumeSlider.addEventListener(TouchEvent.MOUSE_DOWN, onMouseDownV);
			this.volumeSlider.addEventListener(TouchEvent.MOUSE_UP, onMouseUpV);
			this.volumeSlider.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownV);
			this.volumeSlider.addEventListener(MouseEvent.MOUSE_UP, onMouseUpV);
			
			this.wrapper0.addEventListener(MouseEvent.CLICK, buttontouch);
			this.wrapper1.addEventListener(MouseEvent.CLICK, buttontouch);
		}
		
		public function onMouseDown(e:Event):void {
			this._scrubbing = true;
			this.video1.removeEventListener(VideoCurrentTimeEvent.videoCurrentTime, onVideoCurrentTime);
		}
		
		public function onMouseMove(e:Event):void {
			if (this._scrubbing) {
				this.videoSeek = (this.videoDuration * (this.scrubSlider.getValue()));
				this.video1.setSeek(this.videoSeek);
			}
		}
		
		public function onMouseUp(e:Event):void {
			this._scrubbing = false;
			this.video1.addEventListener(VideoCurrentTimeEvent.videoCurrentTime, onVideoCurrentTime);
		}
		
		public function onMouseDownV(e:Event):void {
			_Vscrubbing = true;
		}
		
		public function onMouseUpV(e:Event):void {
			_Vscrubbing = false;
		}
		
		public function buttontouch(e:Event):void {			
			if (toggle_play == 0) {
				this.video1.playVideo();
				pause_btn.visible = true;
				play_btn.visible = false;
				toggle_play = 1;
			} else {			
				this.video1.pauseVideo();
				pause_btn.visible = false;
				play_btn.visible = true;
				toggle_play = 0;
			}
		}
			
		public function onEnterFrame(e:Event):void {		 
			if(_Vscrubbing){
				volumeSlider.alpha = volumeSlider.sliderValue + 0.25; 
			}
		}
		
		/*public override function released(dx:Number, dy:Number, dang:Number):void {
			velX = dx;
			velY = dy;
			velAng = dang;
		}*/		
		
	}
	
}